package com.group2.equipment.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group2.equipment.entity.AddressVO;
import com.group2.equipment.entity.DataDictionary;
import com.group2.equipment.entity.DataDictionaryVO;
import com.group2.equipment.feign.AddressFeign;
import com.group2.equipment.mapper.DataDictionaryMapper;
import com.group2.equipment.service.DataDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author binghan
 * @version 1.0
 * @description: TODO 数据字典业务类
 * @date 2022/6/6 16:48
 */
@Service
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper,DataDictionary> implements DataDictionaryService {

    @Autowired
    AddressFeign addressFeign;

    @Override
    public List<DataDictionaryVO> queryDataDictionary() {

        // 条件查询查全部数据
        List<DataDictionary> dataDictionaries = this.list();
        // 创建返回数据字典值对象
        List<DataDictionaryVO> dataDictionaryVOList = new ArrayList<DataDictionaryVO>();

        // 因为只有二个层级关系 不做递归判断 直接循环写
        for (DataDictionary dataDictionary : dataDictionaries) {
            // 查询等级为 1 的类型id
            if("1".equals(dataDictionary.getLevel().toString())) {
                // 一个大类目 一个VO值对象
                DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
                // 设置大类目名
                dataDictionaryVO.setTypeName(dataDictionary.getDictionaryidName());
                List<String> typeChildName = new ArrayList<>();
                for (DataDictionary dictionary : dataDictionaries) {
                    // 循环遍历查找大类目下的子类型信息
                    if(dataDictionary.getDictionaryId().equals(dictionary.getParentId())) {
                        // 先一个一个加入list集合
                        typeChildName.add(dictionary.getDictionaryidName());
                    }
                }
                // 添加当前类型名下的子类型
                dataDictionaryVO.setTypeChildName(typeChildName);
                dataDictionaryVOList.add(dataDictionaryVO);
            }
        }
        // 新增地址信息
        DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
        // 设置大类目名
        dataDictionaryVO.setTypeName("address_type");
        List<AddressVO> addressVOList = addressFeign.selectAll();
        List<String> typeChildName = new ArrayList<>();
        for (AddressVO a : addressVOList) {
            typeChildName.add(a.getAddressName());
        }
        dataDictionaryVO.setTypeChildName(typeChildName);
        dataDictionaryVOList.add(dataDictionaryVO);
        return dataDictionaryVOList;
    }
}
